package com.soyorin.order.controller;

import com.soyorin.order.entity.ChatItem;
import org.springframework.http.ResponseEntity;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import com.soyorin.order.service.ChatItemService;

@RestController
@RequestMapping("/chat-item")
public class ChatItemController {

    @Autowired
    private ChatItemService baseService;

    // 标准接口一: select-one
    @GetMapping("/{id}")
    @ResponseBody
    public ResponseEntity<ChatItem> selectById(@PathVariable Long id) {
        ChatItem ChatItem = baseService.getById(id);
        if(ChatItem != null) { // return1: 成功查询到数据
            return ResponseEntity.status(200).body(ChatItem);
        } else { // return2: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

    // 标准接口二: select-all
    @GetMapping
    @ResponseBody
    public PageInfo<ChatItem> selectAll(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ChatItem> list = baseService.list();
        return new PageInfo<>(list);
    }

    // 标准接口三: insert
    @PostMapping
    @ResponseBody
    public ResponseEntity<ChatItem> insert(@RequestBody ChatItem ChatItem) {
        boolean result = baseService.save(ChatItem);
        if(result) { // return1: 成功插入数据
            return ResponseEntity.status(200).body(ChatItem);
        } else { // return2: 插入数据失败
            return ResponseEntity.status(500).body(null);
        }
    }

    // 标准接口四: delete
    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseEntity<ChatItem> delete(@PathVariable Long id) {
        ChatItem ChatItem = baseService.getById(id);
        if(ChatItem != null) {
            boolean result = baseService.removeById(id);
            if(result) { // return1: 成功删除数据
                return ResponseEntity.status(200).body(ChatItem);
            } else { // return2: 删除数据失败
                return ResponseEntity.status(500).body(ChatItem);
            }
        } else { // return3: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }

    // 标准接口五: update
    @PutMapping("/{id}")
    @ResponseBody
    public ResponseEntity<ChatItem> update(
            @PathVariable Long id,
            @RequestBody ChatItem ChatItem) {
        ChatItem.setId(id);
        boolean result = baseService.updateById(ChatItem);
        ChatItem updateChatItem = baseService.getById(ChatItem.getId());
        if(result && updateChatItem != null) { // return1: 成功更新数据
            return ResponseEntity.status(200).body(updateChatItem);
        } else { // return2: 更新数据失败
            return ResponseEntity.status(500).body(ChatItem);
        }
    }

    // 标准接口六: select-eq
    @PostMapping("/select-eq")
    @ResponseBody
    public ResponseEntity<List<ChatItem>> selectEq(
            @RequestBody Map<String,Object> conditions) {
        QueryWrapper<ChatItem> wrapper = new QueryWrapper<>();
        for(Map.Entry<String, Object> entry : conditions.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            wrapper.eq(key, value);
        }
        List<ChatItem> list = baseService.list(wrapper);
        if(list != null) { // return1: 成功查询到数据
            return ResponseEntity.status(200).body(list);
        } else { // return2: 未找到对应数据
            return ResponseEntity.status(404).body(null);
        }
    }
}